package org.example.controller;

import lombok.RequiredArgsConstructor;
import org.example.Result;
import org.example.service.SentinelLinkService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/link")
public class SentinelLinkController {


    private final SentinelLinkService sentinelLinkService;

    /***
     * 两个链路
     * 链路1： getUser-> loginApi.getUser
     * 链路2： selectUser-> loginApi.getUser
     * 流控 规则:  链路1 限制  链路2 不限制
     * * @return
     */
    @GetMapping("/getUser")
    public Result<String> getUser() {
        Result<String> user = sentinelLinkService.getUserByUserName("zhangxiaofan");
        if (user.getCode() == 200) {
            return Result.success(user.getData());
        }
        return Result.error(user.getCode(), "Failed to get user");
    }

    @GetMapping("/selectUser")
    public Result<String> selectUser() {
        Result<String> user = sentinelLinkService.getUserByUserName("zhangxiaofan");
        if (user.getCode() == 200) {
            return Result.success(user.getData());
        }
        return Result.error(user.getCode(), "Failed to get user");
    }

}
